<template>
  <!-- 模块1 -->
  <div class="modular">
    <div class="modular_top">
      <div class="triangle"></div>
      <div class="modular_icon">
        <img src="@/assets/img/zjtgslfx.png" alt="">
      </div>
      <h2>近七天供水量分析</h2>
    </div>
    <div class="monitor_echarts">
      <div class="monitor_main"></div>
    </div>
  </div>
</template>
<script>
import * as api from '@/assets/utils/api.js'
export default {
  data() {
    return {
      arr: []
    }
  },
  async created() {
    // 接口参数
    const id = { tenantid: 1128 }
    const res = await api.getSevenDaysWater(id)
    this.arr = res.data
    this.initChart()
    //根据窗口变化自动调节图表大小
    window.addEventListener('resize', () => {
      this.myChart.resize()
    })
  },
  methods: {
    // 图表初始化
    initChart() {
      this.myChart = this.$echarts.init(document.querySelector('.monitor_main'))
      const date = this.arr.fd_date
      for (let i = 0; i < date.length; i++) {
        date[i] = date[i].substring(5, date[i].length)
      }
      const dataArr = this.arr.fd_water_cnt
      // 干吨水能耗
      const arr2 = this.arr.per
      // 同比水量
      const arr3 = this.arr.year_fd_water_cnt
      const maxNum = Math.max.apply(null, [...dataArr, ...arr2, ...arr3]) * 1.5
      let maxArr = []
      for (let i = 0; i < arr2.length; i++) {
        maxArr.push(maxNum)
      }
      this.myChart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'cross' // 默认为直线，可选为：'line' | 'shadow'
          },
          extraCssText: 'background: #023881;border:none;color:#fff;opacity: 0.9',
          formatter(params) {
            // console.log(params)
            return `
            日期：<span style="color: #50D5FD">${params[0].axisValue}</span> <br/>
            供水量：<span style="color: #50D5FD">${params[0].data}</span> <br/>
            干吨水能耗：<span style="color: #50D5FD">${params[2].data}</span> <br/>
            同比水量：<span style="color: #50D5FD">${params[3].data}</span>
            `
          }
        },
        xAxis: {
          type: 'category',
          data: date,
          axisLabel: {
            show: true,
            textStyle: {
              color: '#38e8fb'
            },
            // padding: [0,0,0,-10]
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#80c7e0'
            }
          }
        },
        yAxis: [
          {
            type: 'value',
            position: 'left',
            splitNumber: 6,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#80c7e0'
              }
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#3bdfd3'
              }
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: 'dashed',
                color: '#427fd5'
              }
            },
            axisTick: {
              show: true,
              inside: true,
              length: 3
            }
          },
          {
            type: 'value',
            position: 'right',
            // max: 3,
            // min: -3,
            splitNumber: 6,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#80c7e0'
              }
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#3bdfd3'
              }
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: 'dashed',
                color: '#427fd5'
              }
            },
            axisTick: {
              show: true,
              inside: true,
              length: 3
            }
          },
        ],
        grid: {
          // right: '25%',
          // top: '23%',
          // bottom: '19%',
          // left: '15%',
          // x: "12%",//x 偏移量
          // y: "7%", // y 偏移量
          // width: '100%', // 宽度
          height: '60%'
        },
        legend: {
          data: ['干吨水能耗', '同比水量'],
          top: '5%',
          itemHeight: 8,
          itemWidth: 12,
          textStyle: {
            color: [],
            fontSize: '0.1rem'
          }
        },
        series: [
          {
            data: dataArr,
            type: 'bar',
            // showBackground: true,
            // backgroundStyle: {
            //   color: 'rgba(49, 100, 158, 0.6)'
            // },
            barWidth: 15,
            zlevel: 4,
            itemStyle: {
              normal: {
                //柱形图圆角，初始化效果
                barBorderRadius: [70, 70, 70, 70],
                // 设置颜色渐变

                color: new this.$echarts.graphic.LinearGradient(
                  0, 0, 0, 1,
                  [
                    { offset: 0, color: '#1eb6df' },
                    { offset: 0.5, color: '#39dcd4' },
                    { offset: 1, color: '#4ffcca' },
                  ]
                ),
              }
            },
          },
          {
            data: maxArr,
            type: 'bar',
            barWidth: 30,
            barGap: '-150%',
            zlevel: 3,
            itemStyle: {
              normal: {
                // 设置颜色渐变
                color: 'rgba(49, 100, 158, 0.6)'
              }
            },
          },
          {
            name: '干吨水能耗',
            data: arr2,
            type: 'line',
            smooth: true,
            zlevel: 4,
            showSymbol: true,//是否默认展示圆点
            symbol: 'circle',     //设定为实心点
            symbolSize: 5,   //设定实心点的大小
            itemStyle: {
              normal: {
                color: '#17cbff', //改变折线点的颜色
                lineStyle: {
                  color: '#17cbff', //改变折线颜色
                },
              },
            },
            showBackground: true,
            yAxisIndex: 1
          },
          {
            name: '同比水量',
            data: arr3,
            zlevel: 4,
            type: 'line',
            smooth: true,
            showSymbol: true,//是否默认展示圆点
            symbol: 'circle',     //设定为实心点
            symbolSize: 5,   //设定实心点的大小
            itemStyle: {
              normal: {
                color: '#50e69e', //改变折线点的颜色
                lineStyle: {
                  color: '#50e69e', //改变折线颜色
                },
              },
            },
            showBackground: true,
            yAxisIndex: 1
          },
        ]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.modular {
  // margin: 0.2rem 0;

  .monitor_echarts {
    padding-top: 0.4rem;
    width: 100%;
    height: 85%;

    .monitor_main {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
